package com.atguigu.servlet;

import com.atguigu.domain.Book;
import com.atguigu.domain.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 使用重定向而不是直接调用list()原因
 * 解耦合
 * 让服务器重新发送请求
 * @author dl
 * @create 2020-04-25-14:46
 */
@WebServlet(name = "BookManagerServlet" , urlPatterns = "/book/manager")
public class BookManagerServlet extends BaseServlet {

	private BookService bookService = new BookServiceImpl();

	/**
	 * paging book items
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String currentPage = req.getParameter("current_page");
		// 现在的页面上只是分页 传递的只有 current_page 这个参数
		Page<Book> page = bookService.getPage(currentPage, "4");
		req.setAttribute("list",page);
		page.setUrl("/book/manager?method=page&current_page=");
		req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
	}

	/**
	 * get all book
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	@Deprecated
	protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Book> list = bookService.getAllBook();
		req.setAttribute("list",list);
		req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
	}

	/**
	 * update or add  book
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String currentPage = req.getParameter("current_page");
		// 1 get Book
		Book book = WebUtils.map2Bean(req, new Book());
		if (book.getId() == 0) {
			// save book
			bookService.save(book);
			currentPage = String.valueOf(Integer.MAX_VALUE);
		}else {
			// update book
			bookService.update(book);
		}
		// go to the servlet list()
		resp.sendRedirect(req.getContextPath() + "/book/manager?method=page&current_page="+currentPage);
	}

	/**
	 * get one book
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void getOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 让用户操作之后还能返回当前页 因为是同一次请求 在页面使用 param 内置对象 可以获取到
		String currentPage = req.getParameter("current_page");
		// 1 get Book
		Book book = WebUtils.map2Bean(req, new Book());
		Book one = bookService.getOneBook(book.getId());
		// set to request attribute
		req.setAttribute("book",one);
//		req.setAttribute("current_page",currentPage);
		// go to book_edit.jsp
		req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
	}

	/**
	 * delete book by id
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		String currentPage = req.getParameter("current_page");
		// http 提供的便利 会记录上一次的地址
		String referer = req.getHeader("Referer");
		Book book = WebUtils.map2Bean(req, new Book());
		boolean delete = bookService.delete(book);
		System.out.println("delete()...." + delete);
		// go to list();
//		resp.sendRedirect(req.getContextPath() + "/book/manager?method=page&current_page=" + currentPage);
		resp.sendRedirect(referer);
	}

}
